<%@ page import="com.bw.pojo.Elepro" %>
<%@ page import="java.util.List" %>
<%--
  Created by IntelliJ IDEA.
  User: Aaking-aline
  Date: 2022-04-12
  Time: 10:49
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>信息展示页</title>
        <style>
            table,td {
                border: aquamarine 2px solid;
            }
        </style>
    </head>
    <body>
        <center>
            <table>
                <tr>
                    <td>全选/反选<input type="checkbox" id="selectAll"></td>
                    <td>id</td>
                    <td>电子产品名称</td>
                    <td>电子产品类型</td>
                    <td>电子产品价格</td>
                    <td>电子产品库存</td>
                    <td>电子产品生产日期</td>
                    <td>电子产品产地</td>
                    <td>电子产品IMEI</td>
                    <td>操作</td>
                </tr>

                <%
                    List<Elepro> list = (List<Elepro>) request.getAttribute("list");

                    if(list == null) {
                        response.sendRedirect("SelectEleproServlet?page=1");
                        return ;
                    }

                    for (Elepro elepro : list) { %>
                        <tr>
                            <td><input type="checkbox" class="selectElem" name="<%=elepro.getId()%>"></td>
                            <td><%=elepro.getId()%></td>
                            <td><%=elepro.getElepro_name()%></td>
                            <td><%=elepro.getElepro_kind()%></td>
                            <td><%=elepro.getElepro_price()%></td>
                            <td><%=elepro.getElepro_stock()%></td>
                            <td><%=elepro.getElepro_make_date()%></td>
                            <td><%=elepro.getElepro_make_addr()%></td>
                            <td><%=elepro.getElepro_IMEI()%></td>
                            <td><input type="button" class="deleteElem" name="<%=elepro.getId()%>" value="删除"></td>
                        </tr>
                <%  } %>


            </table>

            <input type="button" id="newElem" value="新增电子产品">

            <input type="button" id="deleteBtn" value="批量删除">


            <input type="button" id="pre" value="上一页">

            <span>页码:</span>

            <span id="page"><%=request.getAttribute("page")%></span>

            <span>总记录数:<%=request.getAttribute("num")%></span>

            <input type="button" id="next" value="下一页">


        </center>
    </body>


    <script>
        document.getElementById("newElem").onclick = function () {
            window.location.href = "insert.jsp";
		}
    </script>

    <script>
        // 单删功能
        let arr = document.getElementsByClassName("deleteElem");

        for(let i = 0;i < arr.length;i++){
        	arr[i].onclick = function () {
                window.location.href = "DeleteServlet?id=" + this.name;
			}
        }
    </script>


    <script>
        // 上一个功能
        document.getElementById("pre").onclick = function () {
        	let page = document.getElementById("page");
        	let nowPage = parseInt(page.innerText) - 1;
            window.location.href = "SelectEleproServlet?page=" + nowPage;
		}
        // 下一页功能
		document.getElementById("next").onclick = function () {
			let page = document.getElementById("page");
			let nowPage = parseInt(page.innerText) + 1;
			window.location.href = "SelectEleproServlet?page=" + nowPage;
		}

    </script>

    <script>
        // 全选反选功能
        document.getElementById("selectAll").onclick = function () {
            let arr = document.getElementsByClassName("selectElem");
            for(let i = 0;i < arr.length;i++){
            	arr[i].checked = this.checked;
            }
		}
    </script>

    <script>
        // 批量删除功能
		document.getElementById("deleteBtn").onclick = function () {
			let str = "";
			let arr = document.getElementsByClassName("selectElem");
			for(let i = 0;i < arr.length;i++){
				if(arr[i].checked){
					str = str + arr[i].name + ",";
                }
			}
			str = str.slice(0,str.length-1);
			window.location.href = "DeleteServlet?id=" + str;
		}
    </script>

</html>
